package com.training.java.core.exceptions;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * The purpose of the demo is to demonstrate --how to use try -catch block.
 * 
 * Objective: Write the text to a specified file. try block: Instantiated
 * FileWriter specifies the location of the file. PrintWriter will print the the
 * text in the file which is specified by 'fileWriter'. Used Write method to
 * write the text in the file. Used close() method to use close the file.
 * 
 * Catch block: Catch block will catch the IO exception printStackTrace() --
 * traces the exception.
 * 
 * 
 * @author Satya
 * 
 */
public class ExceptionFileWriterDemo {

	public static void main(String[] args) {

		FileWriter fileWriter = null;
		PrintWriter out = null;

		try {

			fileWriter = new FileWriter("c:\\javatraining.txt");
			out = new PrintWriter(fileWriter);

			out.write("Write to this file");
			out.write("This is a java document");
			out.close();
		}

		catch (IOException ex) {

			ex.printStackTrace();
		}

		finally {
			if (out != null)

			{

				out.close();

			}

			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
